package com.svms.save.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.svms.save.entitys.SvdaYhb;
import com.svms.save.entitys.SvdaZwgxx;
import com.svms.save.entitys.SvdaZwxxx;
import com.svms.save.services.SvdaYhService;
import com.svms.save.services.SvdaZwgxxService;
import com.svms.save.utils.SysCode;
import com.svms.save.vo.ResultVo;

@Controller
@RequestMapping("/zwg")
public class SvdaZwgxxController {
	
	Logger log = LoggerFactory.getLogger(SvdaZwgxxController.class);
	@Autowired
	private SvdaZwgxxService zwgxxManager;
	@Autowired
	private SvdaYhService yhManager;
	/**
	 * 置物箱编辑（新增/修改）
	 * @param model
	 * @param objectid
	 * @return
	 */
	@RequestMapping("/edit")
	public String toEdit(Model model,Long objectid,Long wzxxOid) {
		model.addAttribute("objectid", objectid);
		model.addAttribute("wzxxOid", wzxxOid);
		return "zwg/edit";
	}
	@RequestMapping("/detailByOid")
	@ResponseBody
	public ResultVo<SvdaZwgxx> detailByOid(Model model,Long objectid){
		ResultVo<SvdaZwgxx> res = null;
		try {
			SvdaZwgxx zwgxx = zwgxxManager.findById(objectid);
			res = ResultVo.success("", zwgxx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误！",e);
			res = ResultVo.systemError("", null);
		}
		return res;
	}
	@RequestMapping("/testZwx")
	@ResponseBody
	public ResultVo<List<SvdaZwxxx>> testZwx(HttpServletRequest request,SvdaZwgxx zwgxx){
		SvdaYhb yh = yhManager.getCurrentUser(request);
		ResultVo<List<SvdaZwxxx>> res = null;
		try {
			List<SvdaZwxxx> list = zwgxxManager.testZwxxx(yh, zwgxx);
			res = ResultVo.success(SysCode.MSG_SAVE_OK, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误！",e);
			res = ResultVo.businessError(SysCode.MSG_SAVE_ERR, null);
		}
		return res;
	}
	
	@RequestMapping("/doSave")
	@ResponseBody
	public ResultVo<String> doSave(HttpServletRequest request,SvdaZwgxx zwgxx){
		ResultVo<String> res = null;
		SvdaYhb yh = yhManager.getCurrentUser(request);
		try {
			int r = zwgxxManager.doSave(yh, zwgxx);
			if(r > 0) {
				res = ResultVo.success(SysCode.MSG_SAVE_OK, "");
			}else {
				res = ResultVo.businessError(SysCode.MSG_SAVE_ERR, "");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("系统错误！",e);
			res = ResultVo.businessError(SysCode.MSG_SAVE_ERR, "");
		}
		return res;
	}
}
